package com.sumer.psychology.comment.infrastructure.persistence.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sumer.psychology.comment.domain.model.CommentType;
import com.sumer.psychology.common.BusinessException;
import com.sumer.psychology.common.persistence.mongodb.AbstractMongoRepository;

/**
 * mongodb的评论资源库
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/10/10
 * Time: 13:42
 */
public class MongoCommentRepository extends AbstractMongoRepository {
    private static final String COMMENT_KEY = "commentId.identity";
    private static final String HOST_KEY = "host.identity";

    private static final String PREFIX_COLLECTION = "sumer_";
    private static final String SUFFIX_COMMENT = "_comment";

    /**
     * 增加评论
     *
     * @param data json
     */
    public void addComment(String data) {
        DBObject comment = initializeCreateData(data, COMMENT_KEY);
        String hostId = nestedValue(comment, HOST_KEY);
        String commentType = commentTypeStringFrom(comment);
        comment.put("host", hostId);
        comment.removeField("commentType");

        db().getCollection(PREFIX_COLLECTION + commentType + SUFFIX_COMMENT).insert(comment);

        increaseReplies(hostId, commentType);
    }

    /*
     * 增加宿主表的reply数量
     */
    private void increaseReplies(String hostId, String commentType) {
        db().getCollection(PREFIX_COLLECTION + commentType).update(
                new BasicDBObject("_id", hostId),
                new BasicDBObject("$inc", new BasicDBObject("replies", 1)),
                true,
                false);
    }

    /**
     * 增加对评论的评论
     *
     * @param data json
     */
    public void addCommentToComment(String data) {
        DBObject commentToComment = initializeCreateData(data, COMMENT_KEY);
        String hostId = nestedValue(commentToComment, HOST_KEY);
        String commentType = commentTypeStringFrom(commentToComment);
        commentToComment.put("host", hostId);
        commentToComment.removeField("commentType");

        db().getCollection(PREFIX_COLLECTION + commentType + SUFFIX_COMMENT)
                .update(
                        idCondition(hostId),
                        new BasicDBObject("$push", new BasicDBObject("comments", commentToComment)));
    }

    private String commentTypeStringFrom(DBObject commentToComment) {
        return nestedValue(commentToComment, "commentType").toLowerCase();
    }

    /**
     * 删除
     *
     * @param id          id
     * @param commentType comment type
     */
    public void removeOneByType(String id, CommentType commentType) {
        db().getCollection(PREFIX_COLLECTION + commentType.toString().toLowerCase() + SUFFIX_COMMENT)
                .remove(new BasicDBObject("_id", id));
    }

    /**
     * 查询唯一
     *
     * @param id          id
     * @param commentType comment type
     * @return json
     */
    public DBObject findOneByType(String id, CommentType commentType) {
        return db().getCollection(PREFIX_COLLECTION + commentType.toString().toLowerCase() + SUFFIX_COMMENT)
                .findOne(new BasicDBObject("_id", id));
    }

    @Override
    public void removeOne(String id) {
        throw new BusinessException("评论必须加入评论类型");
    }

    @Override
    public DBObject findOneWithStatus(String id, String availability) {
        throw new BusinessException("评论必须加入评论类型");
    }

    @Override
    protected String dbName() {
        return null;
    }

}
